﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UniMana.Data
{
    public static class System_GroupData
    {
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="m_data"></param>
        /// <param name="userKey"></param>
        /// <returns></returns>
        public static IQueryable<System_Group> GetGroupByUserKey(DBMain m_data, Guid userKey)
        {
            IQueryable<System_Group> iQuery = from su in m_data.m_DCReadOnly.System_Users
                         from sug in m_data.m_DCReadOnly.System_User_Groups
                         from sg in m_data.m_DCReadOnly.System_Groups
                                              where su.System_UserKey== userKey &&
                         su.System_UserKey == sug.System_UserKey &&
                         sug.System_GroupKey == sg.System_GroupKey
                         select sg;
            return iQuery;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="m_data"></param>
        /// <returns></returns>
        public static IQueryable GetList(DBMain m_data)
        {
            return m_data.m_DCSave.System_Groups;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="m_data"></param>
        /// <param name="_guiUserKey"></param>
        /// <returns></returns>
        public static IQueryable<System_Group> GetGroupNotInUser(DBMain m_data, Guid _guiUserKey)
        {
            IQueryable<System_Group> iQuery = from p in m_data.m_DCReadOnly.System_Groups
                                              where !(

                                                     from su in m_data.m_DCReadOnly.System_Users
                                                     from sug in m_data.m_DCReadOnly.System_User_Groups

                                                     where su.System_UserKey == _guiUserKey &&
                                                     su.System_UserKey == sug.System_UserKey 
                                                     select sug.System_GroupKey).Contains(p.System_GroupKey)

                                              select p;
            return iQuery;
        }
        public static IQueryable GetGroupHaveRule()
        {
            DBMain db = new DBMain();
            var iQuery = (from sg in db.m_DCReadOnly.System_Groups
                          from sgr in db.m_DCReadOnly.System_Group_Rules
                          where
                          sg.System_GroupKey == sgr.System_GroupKey
                          select new
                          {
                              System_RuleKey = "",
                              RuleID = sg.System_GroupKey,
                              RuleName = "",
                              RuleNameVi = sg.Name,
                              Description = sg.Description,
                              ParentID = "",
                              
                          }).Distinct();
            return iQuery;
        }
    }
}
